# -*- coding: utf-8 -*-

from __future__ import unicode_literals
import traceback
import json
from django.http import JsonResponse
from utils.logger import Md_Debug
from django.views.decorators.csrf import csrf_exempt
from utils.response_utils import response_change
from app.plane.common.plane_add_edit_delete import PlaneEditDB
from app.plane.common.plane_query import PlaneManageDB

PlaneEditDB = PlaneEditDB()
PlaneManageDB = PlaneManageDB()


def public_view(request, func, session_content=False):
    data = dict()
    try:
        body = json.loads(request.body)
        if session_content == True:
            body["session_user_id"] = request.session.get("session_user_id")
            body["session_user_name"] = request.session.get("name")
            body["session_company_post"] = request.session.get("company_post")
            body["session_department"] = request.session.get("department")
            if body["session_user_id"] == None:
                data = {"response_msg": "params is error", "response_code": ImportError.IMPORT_CODE_EXECUTE_FAILED}
                raise ("session is error")
        data = func(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)


@csrf_exempt
def add_plane(request):
    return public_view(request, PlaneEditDB.add_plane,True)


@csrf_exempt
def del_plane(request):
    return public_view(request, PlaneEditDB.del_plane)


@csrf_exempt
def edit_plane(request):
    return public_view(request, PlaneEditDB.edit_plane)


@csrf_exempt
def plane_list(request):
    return public_view(request, PlaneManageDB.plane_list,True)

@csrf_exempt
def plane_info(request):
    return public_view(request, PlaneManageDB.plane_info)

@csrf_exempt
def plane_list_in_project(request):
    company_post = request.session.get("company_post")
    if company_post>=3:
        return public_view(request, PlaneManageDB.plane_list_all, True)
    return public_view(request, PlaneManageDB.plane_list_in_project,True)